package com.jhg.drgclinicalpathway.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jhg.drgclinicalpathway.controller.DATA.Jbdz;
import com.jhg.drgclinicalpathway.dao.CommonDao;
import com.jhg.drgclinicalpathway.dao.DRGs_病人分组结果Dao;
import com.jhg.drgclinicalpathway.domain.DRGs_病人分组结果;
import com.jhg.drgclinicalpathway.domain.RpnMsg;
import com.jhg.drgclinicalpathway.domain.menu.CHS_DRGS_enum;
import com.jhg.drgclinicalpathway.domain.menu.CHS_jbbmdz_enum;
import com.jhg.drgclinicalpathway.domain.menu.CHS_ssbmdz_enum;
import drg_group.chongqing_2022.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api("分组器")
@RestController
@Slf4j(topic = "Chs_Group")
@RequestMapping("chs")
public class Chs_Group {
    @Autowired
    CommonDao commonDao;

    @Autowired
    DRGs_病人分组结果Dao GroupResultDao;

    @PostMapping("groupProxy")
    public RpnMsg PathPgDetial(@RequestBody JSONObject brjo){
        System.out.println(brjo);
        // 1.组装分组参数
        MedicalRecord d=new MedicalRecord();
        try  {
            d.setIndex(brjo.getString("jzh"));
            d.setGender(brjo.get("gender").toString());
            d.setAge(brjo.getInteger("age"));
            d.setAgeDay(brjo.getInteger("ageDay"));
            d.setWeight(brjo.getInteger("bodyWeight"));
            d.setDept(brjo.getString("dept"));
            d.setInHospitalTime(brjo.getInteger("inDays"));
            d.setLeavingType(brjo.getString("leavingType"));
            // 国家临床版本转换
            if(brjo.getInteger("chs_t")==0) {
                if(!"".equals(brjo.get("zdList").toString())) {
                    List<String> lszd = (ArrayList<String>) brjo.get("zdList");
                    if(lszd.size()>0) {
                        List<String> zds = new ArrayList<>();
                        int zdsz=lszd.size()>16?16:lszd.size();
                        for(int i=0;i<zdsz;i++) {
                            String ybbm = Jbdz.YBBM.get(lszd.get(i));
                            if(ybbm != null) {
                                zds.add(i, ybbm);
                            } else {
                                zds.add(i,lszd.get(i));
                            }
                        }
                        d.setZdList(zds.toArray(new String[0]));
                        if(!"".equals(brjo.get("ssList").toString())) {
                            List<String> lsss = (ArrayList<String>) brjo.get("ssList");
                            if(lsss.size()>0) {
                                List<String> ss=new ArrayList<String>();
                                int sssz=lsss.size()>8?8:lsss.size();
                                for(int s=0;s<sssz;s++) {
                                    String ssbm=Jbdz.SSBM.get(lsss.get(s));
                                    if(ssbm != null) {
                                        ss.add(s, ssbm);
                                    } else {
                                        ss.add(s, lsss.get(s));
                                    }
                                }
                                d.setSsList(ss.toArray(new String[0]));
                            } else {
                                d.setSsList(new String[0]);
                            }
                        } else {
                            d.setSsList(new String[0]);
                        }
                    } else {
                        return RpnMsg.FAIL("主要诊断不能为空");
                    }
                } else {
                    return RpnMsg.FAIL("主要诊断不能为空");
                }
            } else {
                List<String> lszd = (ArrayList<String>) brjo.get("zdList");
                int zdsz=lszd.size()>16?16:lszd.size();
                List<String> zds = new ArrayList<>();
                for(int i=0;i<zdsz;i++) {
                    zds.add(lszd.get(i));
                }
                d.setZdList(zds.toArray(new String[0]));
                List<String> lsss = (ArrayList<String>) brjo.get("ssList");
                if(lsss.size()>0) {
                    List<String> ss=new ArrayList<String>();
                    int sssz=lsss.size()>8?8:lsss.size();
                    for(int s=0;s<sssz;s++) {
                        ss.add(lsss.get(s));
                    }
                    d.setSsList(ss.toArray(new String[0]));
                } else {
                    d.setSsList(new String[0]);
                }
            }
            d.setRemark(brjo.getString("brxm"));
        } catch (Exception ex) {
            return RpnMsg.FAIL("参数错误！");
        }
        // 2.开始分组
        GroupProxy Grouper = new GroupProxy();
        GroupResult rs= Grouper.group_record(d);
        // 3.组装分组信息
        JSONObject date=new JSONObject();
        date.put("MdcGroup", rs.mdc);
        date.put("AdrgGroup", rs.adrg);
        date.put("Dtatus", rs.status);
        date.put("Message", rs.messages);
        if("分组成功".equals(rs.status)) {
            CHS_DRGS_enum chsDrgs = CHS_DRGS_enum.valueOf(rs.drg);
            date.put("DrgGroup", rs.drg);
            date.put("GroupName", Base.DRG.get(rs.drg));
            date.put("Weight", chsDrgs.getQz());
            date.put("StayDays", chsDrgs.getTs());
            date.put("StayFee", chsDrgs.getFee());
        } else if("QY".equals(rs.adrg)) {
            date.put("DrgGroup", "QY"+rs.drg.substring(0,1)+"组");
            date.put("GroupName", rs.drg.substring(0,1)+"_歧义组");
            date.put("Weight", 0);
            date.put("StayDays", 0);
            date.put("StayFee", 0);
        } else {
            date.put("DrgGroup", rs.drg);
            date.put("GroupName", "0000组");
            date.put("Weight", 0);
            date.put("StayDays", 0);
            date.put("StayFee", 0);
        }
        // 4.保存分组结果
        if(!"".equals(brjo.getString("jzh")) && brjo.getInteger("save_ly")>0) {
            // 结果来源3，测试4
            int ly=brjo.getInteger("save_ly");
            QueryWrapper<DRGs_病人分组结果> queryWrapper = new QueryWrapper<DRGs_病人分组结果>();
            queryWrapper.eq("病人标识", brjo.getString("jzh"));
            queryWrapper.eq("来源", ly);
            GroupResultDao.delete(queryWrapper);
            // 删除历史结果重新保存
            DRGs_病人分组结果 dr = new DRGs_病人分组结果();
            dr.setID(UUID.randomUUID().toString());
            dr.set病人标识(brjo.getString("jzh"));
            dr.set参数(brjo.toString());
            dr.set记录时间(new Date());
            dr.set来源(ly);
            dr.set日志(rs.messages.toString());
            if("分组成功".equals(rs.status)) {
                dr.set编码(rs.drg);
                CHS_DRGS_enum chsDrgs = CHS_DRGS_enum.valueOf(rs.drg);
                dr.set床日(chsDrgs.getTs());
                dr.set总费用(chsDrgs.getFee());
            } else if("QY".equals(rs.adrg)) {
                dr.set编码("QY"+rs.drg.substring(0, 1)+"组");
                dr.set床日(0);
                dr.set总费用(0.0d);
            } else {
                dr.set编码("0000组");
                dr.set床日(0);
                dr.set总费用(0.0d);
            }
            GroupResultDao.insert(dr);
        }
        return RpnMsg.SUCCESS(date.toString());
    }

    @PostMapping("preInfo")
    public RpnMsg GetDrigenListByParam(@RequestBody Map<String,Object> map) {
        int lb = (int) map.get("lb");
        String key = (String)map.get("key");
        List mslist=null;
        if(lb==1) {
            // 检索诊断记录
            mslist = GroupResultDao.getDrigneList(key);
        } else if (lb==2) {
            // 检索手术记录
            mslist = GroupResultDao.getSSList(key);
        } else if(lb==3) {
            // 获取相关手术
            mslist = GroupResultDao.getXgSSList(key);
        } else if(lb==4) {
            // 获取手术记录
            mslist = GroupResultDao.getBrSSList(key);
        }
        return RpnMsg.SUCCESS(mslist);
    }
}
